limsBatch.editPage = {

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-batch-edit-list-grid',
            url: limsBatch.baseUrl + '/queries/raw',
            requestData: {
                createdById_EQ: workspace.user.id,
                status_SB: 'draft'
            },
            service: 'limsBatchServiceImpl',
            dbTable: 'T_LIMS_BATCH',
            columns: limsBatch.getEditListColumns(),
            deleteFormatter: function (row) {
                return row.batchCode || row.id;
            },
            onCellClick: function (field, row) {
                field === 'batchCode' && Gikam.loadBatchDetailPageV12(row.id);
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'GIKAM.BUTTON.INSERT',
                        fields: limsBatch.getBaseInfoFormFields(),
                        url: limsBatch.baseUrl
                    }).done(function (id) {
                        var row = {}
                        let url = Gikam.printf(limsBatch.baseUrl + '/{id}', {id: id})
                        Gikam.getTextSync(url).done(data => {
                            row = JSON.parse(data)
                        })
                        if (row.id) {
                            Gikam.loadBatchDetailPageV12(row.id)
                        }
                        Gikam.getComp('lims-batch-edit-list-grid').refresh();
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('lims-batch-edit-list-grid').deleteRows(limsBatch.baseUrl);
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.EXPORT',
                icon: 'export',
                onClick: function () {
                    Gikam.createLimsModal({
                        width: 400,
                        height: 180,
                        onConfirm: function () {
                            var form = Gikam.getComp('choose-batchtype-form');
                            if (form.validate()) {
                                var data = form.getData()
                                var tmpCode = 'LimsBatchExportTemplate-' + data.batchType
                                Gikam.getText(Gikam.printf(IFM_CONTEXT + '/core/module/item/files/template-urls' + Gikam.param({
                                    templateCode: tmpCode,
                                    service: 'LimsBatchExportTemplate',
                                    ids: '',
                                    batchType: data.batchType
                                }))).done(function (url) {
                                    Gikam.getLastModal().close()
                                    Gikam.download(url)
                                });
                            }
                        },
                        m: {
                            type: 'form',
                            id: 'choose-batchtype-form',
                            disableNotEmptyValidate: true,
                            fields: [{
                                field: 'batchType',
                                title: 'T_LIMS_BATCH.BATCHTYPE',
                                preInsert: true,
                                type: 'select',
                                firstBlank: false,
                                category: 'batchType',
                                validators: ['notEmpty'],
                            },],
                            columns: 1
                        },
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.IMPORT',
                icon: 'import',
                onClick: function () {
                    Gikam.create('simpleUploader', {
                        id: 'batch-info-uploader',
                        dbTable: 'T_LIMS_BATCH',
                        bizId: 'import',
                        autoClose: true,
                        multiple: false,
                        accept: ['xls'],
                        onAfterClose: function (files) {
                            if (Gikam.isNotEmpty(files)) {
                                workspace.window.showMask()
                                let url = Gikam.printf(IFM_CONTEXT + '/core/module/item/files/{id}/action/import/' + 'LimsBatchImportFileFunction', {
                                    id: files[0].id
                                })
                                Gikam.post(url).done(req => {
                                    Gikam.getComp('lims-batch-edit-list-grid').refresh()
                                    if (req && req.message) {
                                        Gikam.alert(req.message)
                                    }
                                    if (req.url) {
                                        Gikam.download(req.url)
                                    }
                                }).always(function () {
                                    workspace.window.closeMask()
                                })
                            }
                        }
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick: function () {
                    var selections = Gikam.getComp('lims-batch-edit-list-grid').getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    var submitUrl = limsBatch.baseUrl + '/submit';
                    Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.SUBMIT', function () {
                        workspace.window.showMask()
                        Gikam.postText(submitUrl, Gikam.getJsonWrapper(null, [null, selections])).done(function () {
                            Gikam.getComp('lims-batch-edit-list-grid').refresh();
                        }).always(function () {
                            workspace.window.closeMask()
                        })
                    })
                }
            }]
        }
    },

    getSubmittedGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-batch-edit-list-grid-submitted',
            url: limsBatch.baseUrl + '/queries',
            page: true,
            requestData: {
                createdById_EQ: workspace.user.id,
                status_EQ: 'done',
            },
            columns: limsBatch.getEditListColumns(),
            onCellClick: function (field, row) {
                field === 'batchCode' && Gikam.loadBatchDetailPageV12(row.id, true);
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.UNDO',
                icon: 'disable',
                onClick: function () {
                    _this.recall()
                }
            }],
        }
    },

    /**
     * 召回订单，要求订单下所有样本都还是在核对入库界面
     */
    recall: function () {
        var _this = this
        var grid = Gikam.getComp('lims-batch-edit-list-grid-submitted')
        var selections = grid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM')
            return
        }
        Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.SUBMIT', () => {
            var url = limsBatch.baseUrl + '/recall-batch'
            Gikam.postText(url, Gikam.getJsonWrapper(null, [null, selections])).done(req => {
                req = JSON.parse(req)
                if (req.flag) {
                    Gikam.getComp('lims-batch-edit-list-grid-submitted').refresh()
                }
                if (req.message) {
                    Gikam.alert(req.message)
                }
            })
        })
    },

    getMainTab: function () {
        var _this = this;
        var first = false
        return {
            type: 'tab',
            id: 'main-tab',
            panels: [{
                title: 'BASEMODULE.RM.ENVIRONMENTS.TAB.UN_SUBMIT_TAB',
                id: 'lims-batch-tab-not-submit',
                items: [_this.getGridParam()]
            }, {
                title: 'BASEMODULE.RM.ENVIRONMENTS.TAB.SUBMITTED_TAB',
                id: 'lims-batch-tab-submitted',
                items: [_this.getSubmittedGridParam()]
            }],
            onActived: function (data) {
                switch (data.index) {
                    case 0:
                        if (first) {  // 首次加载页面时，不刷新，避免进来就刷新两次表格
                            Gikam.getComp('lims-batch-edit-list-grid').refresh()
                        } else {
                            first = true
                        }
                        break
                    case 1:
                        Gikam.getComp('lims-batch-edit-list-grid-submitted').refresh()
                        break
                }
            }
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-batch-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getMainTab()]
            }
        });
    },

    init: function () {
        this.create();
    }
};
